/**
 * 用户详情对话框
 */
var UserInfoDlg = {
    data: {
        deptId: "",
        deptName: ""
    }
};

layui.use(['layer', 'form', 'admin', 'laydate', 'ax','layarea'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var layer = layui.layer;
    var layarea = layui.layarea;
    var examContent = [];
    layarea.render({
        elem: '#area-picker',
        change: function (res) {
            console.log(res);
        }
    });
    new $ax(Feng.ctxPath + "/api/exam/getExamMonth?soldOut=1&createUser="+Feng.getUrlParam("createUser"),
        function (data) {
            var str = '<option value="">请选择</option>';
            data.data.forEach(function (d){
                str=str+"<option value="+d.examMonthId+" >"+d.month+"</option>";
            });
            $("#examMonthId").html(str);
            form.render();
        }).start();
    new $ax(Feng.ctxPath + "/api/exam/getExamPlace?soldOut=1&createUser="+Feng.getUrlParam("createUser"),
        function (data) {
            var str = '<option value="">请选择</option>';
            data.data.forEach(function (d){
                str=str+"<option value="+d.examPlaceId+" >"+d.place+"</option>";
            });
            $("#examPlaceId").html(str);
            form.render();
        }).start();
    new $ax(Feng.ctxPath + "/exam/examList?examId="+Feng.getUrlParam("examId"), function (data) {
        var datum = data.data[0];
        console.log(datum);
        if(datum.examPlaceCost >0){
            datum.examPlace = 1;
            $("#placeCost").css("display", "inline");
        }else {
            datum.examPlace = 0;
        }
        if(datum.examServiceCost >0){
            datum.examService = 1;
            $("#serviceCost").css("display", "inline");
        }
        if(datum.examContent){
            var textJson = JSON.parse(datum.examContent)
            for (var i = 0; i < textJson.length; i++) {
                var content =
                    `<div class="layui-form-item">
                        <label class="layui-form-label">考试时间/内容<span style="color: red;">*</span></label>
                        <div class="layui-input-block">
                            <input name="examText`+i+`" value="`+textJson[i].examText+`" type="text" class="layui-input" />
                        </div>
                        <div class="layui-input-inline" style="margin-top: 10px">
                            <input name="examTime`+i+`" value="`+textJson[i].examTime+`" type="datetime-local"/>
                        </div>
                        <button type="button" class="layui-btn layui-btn-sm"  style="margin: 10px 0 0 10px" id="remove">
                            <i class="layui-icon">&#xe640;</i>
                        </button>
                    </div>`;
                $("#content").append(content);
            }
        }
        form.val('userForm', datum);
    }).start();


    let num = 1;
    $('#btn').click(function () {
        num ++;
        var content =
            `<div class="layui-form-item">
                <label class="layui-form-label">考试时间/内容<span style="color: red;">*</span></label>
                <div class="layui-input-block">
                    <input name="examText${num}" placeholder="请输入内容" type="text" class="layui-input" />
                </div>
                <div class="layui-input-inline" style="margin-top: 10px">
                    <input name="examTime${num}" placeholder="请输入时间" type="datetime-local"/>
                </div>
                <button type="button" class="layui-btn layui-btn-sm"  style="margin: 10px 0 0 10px" id="remove">
                    <i class="layui-icon">&#xe640;</i>
                </button>
            </div>`;
        $("#content").append(content);
    });
    $('#content').on('click',' #remove',function () {
        $(this).parent().remove();
    })
// 收费模式
    form.on('select(examPlace)', function (data) {
        if (data.value == 1) {
            $("#placeCost").css("display", "inline");
        } else {
            $("#examPlaceCost").val("");
            $("#placeCost").css("display", "none");
        }
    })
    // 收费模式
    form.on('select(examService)', function (data) {
        if (data.value == 1) {
            $("#serviceCost").css("display", "inline");
        } else {
            $("#examServiceCost").val("");
            $("#serviceCost").css("display", "none");
        }
    })

    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        let arr = [];
        for(let k in data.field) {
            if(data.field.hasOwnProperty(k)) {
                if(k.includes('examText')) {
                    let n = k.substr(8);
                    let examTime = 'examTime' + n;
                    arr.push({examText: data.field[k],examTime: data.field[examTime]})
                }
            }
        }
        data.field.examContent = JSON.stringify(arr);
        var ajax = new $ax(Feng.ctxPath + "/exam/examSave", function (data) {
            Feng.success("添加成功！");
            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);
            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();
    });
});